package com.wazzapps.sdk.api;

import android.util.Log;

import com.wazzapps.sdk.WazzUserInfo;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Service {
    private static final String LOG_TAG = "WazzSdk";
    private static Service ourInstance = new Service();
    private int appVersionCode;
    private Connector connector;
    private Queue<Event> eventQueue = new LinkedList();
    private boolean isInited = false;
    private ScheduledExecutorService messageSenderThread = Executors.newSingleThreadScheduledExecutor();
    private Queue<AbstractMap.SimpleEntry<String, Map<String, Object>>> onInitQueue = new LinkedList();
    private String sessionId;
    private long sessionLength = 0;
    private long sessionStartTime;
    private WazzUserInfo user;

    public static Service getInstance() {
        return ourInstance;
    }

    private Service() {
    }

    public void init(String str, String str2, String str3, String str4, int i, WazzUserInfo wazzUserInfo) {
        Log.i(LOG_TAG, "Init");
        this.appVersionCode = i;
        this.connector = new Connector(str, str2, str3, str4, wazzUserInfo);
        this.user = wazzUserInfo;
        resumeSession();
        this.messageSenderThread.scheduleAtFixedRate(new Runnable() { // from class: com.wazzapps.sdk.api.Service.1
            @Override // java.lang.Runnable
            public void run() {
                Service.this.flushEvents();
            }
        }, 2, 5, TimeUnit.SECONDS);
    }

    public void resumeSession() {
        if (this.sessionId == null) {
            this.sessionId = UUID.randomUUID().toString();
        }
        this.sessionStartTime = System.currentTimeMillis();
    }

    public long getSessionLength() {
        return (this.sessionLength + System.currentTimeMillis()) - this.sessionStartTime;
    }

    public void flushEvents() {
        if (!this.isInited) {
            onInit();
        }
        ArrayList arrayList = new ArrayList();
        while (true) {
            Event event = getEvent();
            if (event == null) {
                break;
            }
            arrayList.add(event);
        }
        if (arrayList.size() > 0) {
            this.connector.sendStatMessage((Event[]) arrayList.toArray(new Event[arrayList.size()]));
        }
    }

    public void onInit() {
        if (!this.isInited && this.user.getUserAdvertisingId() != null) {
            this.isInited = true;
            this.isInited = true;
            while (true) {
                AbstractMap.SimpleEntry<String, Map<String, Object>> poll = this.onInitQueue.poll();
                if (poll != null) {
                    String key = poll.getKey();
                    poll.getValue();
                    Log.i(LOG_TAG, "OnInitTask: " + key);
                    key.hashCode();
                } else {
                    return;
                }
            }
        }
    }

    public void addEvent(Event event) {
        this.eventQueue.add(event);
    }

    public Event getEvent() {
        return this.eventQueue.poll();
    }

    public String getSessionId() {
        return this.sessionId;
    }

    public int getAppVersionCode() {
        return this.appVersionCode;
    }
}